package com.itheima.wisdomnext.mapper;

import com.itheima.wisdomnext.entity.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {

    // 根据帖子 ID 获取所有评论
    @Select("SELECT * FROM comments WHERE post_id = #{postId} AND banned != 1 AND parent_id IS NULL")
    List<Comment> findByPostId(Integer postId);

    // 根据评论 ID 查找评论
    @Select("SELECT * FROM comments WHERE id = #{id}")
    Comment getCommentById(Integer id);

    // 根据父评论 ID 获取所有回复
    @Select("SELECT * FROM comments WHERE parent_id = #{parentId} AND banned != 1")
    List<Comment> findByParentId(Integer parentId);

    // 插入一条评论
    @Insert("INSERT INTO comments (post_id, userid, content, image_url, parent_id, likes) " +
            "VALUES (#{postId}, #{userId}, #{content}, #{imageUrl}, #{parentId}, #{likes})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertComment(Comment comment);

    // 更新评论
    @Update("UPDATE comments SET likes = #{likes} WHERE id = #{id}")
    void updateComment(Comment comment);

    // 删除评论
    @Delete("DELETE FROM comments WHERE id = #{id}")
    void deleteComment(@Param("id") Integer id);

    // 获取特定评论的回复数
    @Select("SELECT COUNT(*) FROM comments WHERE parent_id = #{commentId} AND banned != 1")
    int selectReplyCountByCommentId(@Param("commentId") Integer commentId);

    //检查用户是否点赞了特定评论
    @Select("SELECT COUNT(*) FROM likes WHERE target_id = #{commentId} AND userid = #{userId} AND target_type = 'COMMENT'")
    int isCommentLikedByUser(@Param("commentId") Integer commentId, @Param("userId") Integer userId);
}